<?php
if (!isset($_SESSION))
    session_start();
if (isset($_GET["logout"]) && isset($_SESSION["logged_user"])) {
    unset($_SESSION["logged_user"]);
    $_SESSION["logged_bool"] = false;
}
require_once("loginclass.php");

require('../../Smarty/libs/Smarty.class.php');

$smarty = new Smarty;

$username = "";
$logged = false;
$formUserValue = "enter user name here...";
if (isset($_POST["user"]))
    $formUserValue = $_POST["user"];
if (isset($_SESSION["logged_user"])) {
    $username = $_SESSION["logged_user"];
    $_SESSION["logged_bool"] = true;
    $logged = true;
    if (!empty($_SESSION['currentPage'])) {
        $_SESSION['login_guard']['postcontents'] = $_POST;
        $url = $_SESSION['currentPage'];
    }
    else {
        $url = 'index.php';
    }
    header("Location: ". $url);
    exit();
} ?>
﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <?php
    $created = false;
    if (isset($_SESSION["account_created"])) {
        unset($_SESSION["account_created"]);
        $created = true;
    }
    $smarty->assign("created", $created);
    $smarty->assign("logged", $logged);
    $smarty->assign("username", $username);
    $smarty->assign("formUserValue", $formUserValue);
    
    if (empty($_SESSION['login_guard']['postcontents']['currentPage']) && empty($_SESSION['currentPage'])) {
        $smarty->assign('userReferred', FALSE);
    }
    else {
        $smarty->assign('userReferred', TRUE);
    }
    if (!empty($_SESSION['login_guard']['postcontents']['currentPage']) || !empty($_SESSION['currentPage'])) {
       /*
        * If a user has failed to log in the data we need is in his post data
        */
        if(isset($_SESSION['loginFail']) && ($_SESSION['loginFail'] == TRUE)){
            $_SESSION['login_guard']['postcontents'] = $_POST;
        }
        if(isset($_SESSION['loginFail'])){ 
            unset($_SESSION['loginFail']); 
        }
       /* 
        * User comes from another page - used to determine if extra hidden fields are necessary 
        * Used to send the user to the refferee page after login 
        */

        $currentPage = $_SESSION['login_guard']['postcontents']['currentPage'];

        /*
         * Assign the variables in the _POST superglobal (ultimately countained
         * in the _SESSION superglobal) to Smarty template variables IF the 
         * variables are set
         */
        
        /* Smarty assignment - used to generate hidden input in the login form */
        
        $smarty->assign('currentPage',      $_SESSION['login_guard']['postcontents']['currentPage']);
        $smarty->assign('hotelID',          $_SESSION['login_guard']['postcontents']['hotelId']);
        $smarty->assign('supplierType',     $_SESSION['login_guard']['postcontents']['supplierType']);
        $smarty->assign('roomRateKey',      $_SESSION['login_guard']['postcontents']['roomRateKey']);
        $smarty->assign('roomRateCode',     $_SESSION['login_guard']['postcontents']['roomRateCode']);
        $smarty->assign('roomTypeCode',     $_SESSION['login_guard']['postcontents']['roomTypeCode']);
        $smarty->assign('hotelImage',       $_SESSION['login_guard']['postcontents']['hotelImage']);
        $smarty->assign('hotelName',        $_SESSION['login_guard']['postcontents']['hotelName']);
        $smarty->assign('roomDescription',  $_SESSION['login_guard']['postcontents']['roomDescription']);
        $smarty->assign('total',            $_SESSION['login_guard']['postcontents']['total']);
        $smarty->assign('averageRate',      $_SESSION['login_guard']['postcontents']['averageRate']);
        $smarty->assign('roomtotal',        $_SESSION['login_guard']['postcontents']['roomtotal']);
        $smarty->assign('nightlyRateTotal', $_SESSION['login_guard']['postcontents']['nightlyRateTotal']);
        $smarty->assign('surchargeTotal',   $_SESSION['login_guard']['postcontents']['surchargeTotal']);
        $smarty->assign('currencyCode',     $_SESSION['login_guard']['postcontents']['currencyCode']);
       
        /* Destroys the session variables for safety reasons */
        unset($_SESSION['login_guard']);

        $_SESSION['currentPage'] = $currentPage;
    }
    /*
     * User does not come from another page - used to determine if extra 
     * hidden fields are necessary 
     */


    $smarty->setTemplateDir('./smarty/templates/');
    $smarty->setCompileDir('./smarty/templates_c/');
    $smarty->setConfigDir('./smarty/configs/');
    $smarty->setCacheDir('./smarty/cache/');

    $smarty->display('preHeader.tpl');
    echo '<body>';
    $smarty->assign('menuPage', "Log-In");
    $smarty->display('header.tpl');
    $smarty->display('loginPage.tpl');
    $smarty->display('flashyBox.tpl');
    $smarty->display('footer.tpl');
    ?>

    <script type="text/javascript">
        $(document).ready(function() {
<?php if ($login->getStatus() == 1) { ?>
            $("#login_error").css("visibility","visible");
<?php } ?>
    });
    </script>
</body>

</html>
